import 'package:flutter/material.dart';

class MyImage extends StatefulWidget {
  MyImage({this.imgSrc,this.fit});

  final String imgSrc;
  final BoxFit fit;

  @override
  _MyImageState createState() => _MyImageState();
}

class _MyImageState extends State<MyImage> {
  @override
  Widget build(BuildContext context) {
    return FadeInImage(
      image:NetworkImage(widget.imgSrc),
      fit: widget.fit??BoxFit.cover,
      placeholder:AssetImage('images/placeholder.png'),
      placeholderErrorBuilder: (
          BuildContext context,
          Object error,
          StackTrace stackTrace,
          ){
        print('error');
        print(error);
        return Image.asset('images/image_error.png');
      },
    );
    return Image.network(
      widget.imgSrc,
      fit: widget.fit??BoxFit.cover,
      frameBuilder: (BuildContext context, Widget child, int frame, bool wasSynchronouslyLoaded) {
        if (wasSynchronouslyLoaded) {
          return child;
        }
        return AnimatedOpacity(
          child: child,
          opacity: frame == null ? 0 : 1,
          duration: const Duration(seconds: 1),
          curve: Curves.easeOut,
        );
      },
      loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent loadingProgress) {
        if (loadingProgress == null)
          return child;
        return Center(
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
            value: loadingProgress.expectedTotalBytes != null
                ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes
                : null,
          ),
        );
      },
      errorBuilder: (
          BuildContext context,
          Object error,
          StackTrace stackTrace,
      ){
        print('error');
        print(error);
        return Image.asset('images/image_error.png');
      },
    );
  }
}
